﻿using System;
using System.IO;

namespace Sync.Log
{
    /// <summary>
    /// This class creates a .log file for storing log data.
    /// Different Log types are Error, Warming, and Info
    /// </summary>
    public static class FileLog
    {
        public const string LogPath = @"..\..\Sync.log";
        public static StreamWriter Stream { get; private set; }

        /// <summary>
        /// Creates a StreamWriter.
        /// Writes to the Log file with the message date and log type. 
        /// Flushes and closes the stream.
        /// </summary>
        /// <param name="message">message you would like to be appended.</param>
        /// <param name="type">Type of Log this is. Error, Warning, Info</param>
        public static void LogMessage(string message, LogType type = LogType.Info)
        {
            if (!System.IO.File.Exists(Path.GetFullPath(LogPath)))
            {
                System.IO.File.Create(Path.GetFullPath(LogPath)).Close();
                LogMessage("Log file not found. New log file created in " + Path.GetFullPath(LogPath), LogType.Warning);
            }
            
            Stream = new StreamWriter(LogPath, true);
            Stream.WriteLine(AcquireDate() + GetLogType(type) + message);
            Stream.Flush();
            Stream.Close();
        }

        /// <summary>
        /// Grabs the Needed date and puts brackets around the date/time.
        /// </summary>
        /// <returns></returns>
        private static string AcquireDate()
        {
            return "[" + CurrentDate() + "] ";
        }

        /// <summary>
        /// Gets the current time and date.
        /// </summary>
        /// <returns>Current time/date</returns>
        private static DateTime CurrentDate()
        {
            return DateTime.Now;
        }

        /// <summary>
        /// Returns the string value of log type.
        /// </summary>
        /// <param name="type">type of log</param>
        /// <returns>string representation of log type</returns>
        public static string GetLogType(LogType type)
        {
            switch (type)
            {
                case LogType.Error:
                    return "[ERROR] ";
                case LogType.Info:
                    return "[INFO] ";
                case LogType.Warning:
                    return "[WARNING] ";
                default:
                    return "[OTHER] ";
            }
        }
    }
}